<?php

class Widget_ProductCategory extends WP_Widget {

	function Widget_ProductCategory() {
		$widget_ops = array( 'classname' => 'widget_product_category', 'description' => __( "Danh mục sản phẩm" ) );
		parent::WP_Widget( false, __( 'Danh mục sản phẩm', 'woothemes' ), $widget_ops );
	}

	function widget( $args, $instance ) {
		extract( $args );

		$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base);
		echo $before_widget;
		if ( $title )
			echo $before_title . $title . $after_title;

	       /* Show content widget */
           echo $this->get_dropdown_category("danh-muc");
           ?>
           <script type='text/javascript'>
            /* <![CDATA[ */
            	jQuery(function ( $ ){
            	   $(".categories").find("li.active").parent("ul").addClass("open"); 
                                       
            	   $(".categories li a").click(function(){
            	       var child = $(this).parent("li").children("ul");
                       if(child.length>0){
                            var is_open = $(this).parent("li").children("ul.open");
                            if(is_open.length>0) return true;
                            $(".categories").find("ul").slideUp("slow").removeClass("open");
                            child.slideDown("slow").addClass("open");
                            return false;
                       }else{
                            return true;
                       }
            	   });
            	});
            /* ]]> */
            </script>
           <?php 
           
		echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		return $instance;
	}

	function form( $instance ) {
		//Defaults
		$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
		$title = esc_attr( $instance['title'] );
?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<?php
	}
    
    /* Lay danh muc da cap */
    public function get_dropdown_category($taxonomy) {
        $terms = get_terms($taxonomy,array( 'orderby' => 'custom_sort', 'order' => 'ASC', 'hide_empty'=> false,'parent' => 0 ));
        $curent_cat = get_query_var('term');
        
        $output = "
        <ul class='categories'>";
        foreach($terms as $term){
            $output.="
            <li".(($curent_cat==$term->slug)?" class='active'":"")."><a href='".get_term_link($term,$taxonomy)."'>".$term->name."</a>
                ".$this->get_category_child($term,$taxonomy)."
            </li>";
        }
        $output .="
        </ul>";
        return $output;
    }
    
    private function get_category_child($parent , $taxonomy) {
        $terms = get_terms($taxonomy,array('orderby'=> 'custom_sort', 'order' => 'ASC','hide_empty'=> false,'parent'=> $parent->term_id));
        
        $curent_cat = get_query_var('term');
        
        $output="";                
        if(!empty($terms)) {
            $output = "
            <ul".(($curent_cat==$parent->slug)?" class='open'":"").">";
            foreach($terms as $term){
                $output.="
                <li".(($curent_cat==$term->slug)?" class='active'":"")."><a href='".get_term_link($term,$taxonomy)."'>".$term->name."</a>
                    ".$this->get_category_child($term,$taxonomy)."
                </li>";
            }
            $output .="
            </ul>";
        }
        return $output;
    }

}

register_widget( 'Widget_ProductCategory' );


?>